﻿Imports System.Data.OleDb
Public Class details1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

  Protected Sub Ins_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Ins.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(image.FileName)
        If image.HasFile Then
            image.SaveAs(Server.MapPath("pictures") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO tips (Skincaretips,Imagetips,skincare_fkid) values (@f1,@f2,@f3)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("skinConnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", t2.Text)
        cmd.Parameters.AddWithValue("@f2", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f3", Request.QueryString("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))

    End Sub
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
    Protected Sub comment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles comment.Click
        Dim sql As String = "INSERT INTO feedback (username,feedback,comment,tips_fkid) values (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("skinConnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", f1.Text)
        cmd.Parameters.AddWithValue("@f2", f2.Text)
        cmd.Parameters.AddWithValue("@f3", f3.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tag.Click
        Dim sql As String = "INSERT INTO skincaretags (skincare_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("skinConnectionstring").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cbl.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
    End Sub

    Protected Sub Addtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Addtag.Click
        Dim sql As String = "INSERT INTO Tags (TagName) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("skinConnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb2.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID Desc", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)

        Dim associate As String = "INSERT INTO SkincareTags (skincare_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()
    End Sub
End Class